import {
    Controller,
    Get,
    Post,
    Body,
    Patch,
    Param,
    Delete,
    UseGuards,
    Request,
    Query,
    HttpStatus,
    ForbiddenException,
    NotFoundException
} from '@nestjs/common';
import { ArtworksService } from './artworks.service';
import { CreateArtworkDto } from './dto/create-artwork.dto';
import { UpdateArtworkDto } from './dto/update-artwork.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles } from '../auth/decorators/roles.decorator';
import { UserRole } from '../users/schemas/user.schema';
import { Public } from '../auth/decorators/public.decorator';

@Controller('artworks')
export class ArtworksController {
    constructor(private readonly artworksService: ArtworksService) { }

    @Post()
    @UseGuards(JwtAuthGuard)
    async create(@Request() req, @Body() createArtworkDto: CreateArtworkDto) {
        return this.artworksService.create({
            ...createArtworkDto,
            user_id: req.user.userId,
        });
    }

    @Get()
    @Public()
    async findAll(
        @Query('page') page = 1,
        @Query('limit') limit = 10,
        @Query('sort') sort = 'newest'
    ) {
        return this.artworksService.findAll(+page, +limit, sort);
    }

    @Get('featured')
    @Public()
    async findFeatured() {
        return this.artworksService.findFeatured();
    }

    @Get(':id')
    @Public()
    async findOne(@Param('id') id: string) {
        return this.artworksService.findOne(id);
    }

    @Patch(':id')
    @UseGuards(JwtAuthGuard)
    async update(
        @Request() req,
        @Param('id') id: string,
        @Body() updateArtworkDto: UpdateArtworkDto
    ) {
        return this.artworksService.update(id, req.user.userId, updateArtworkDto);
    }

    @Delete(':id')
    @UseGuards(JwtAuthGuard)
    async remove(@Request() req, @Param('id') id: string) {
        return this.artworksService.remove(id, req.user.userId);
    }

    @Patch(':id/publish')
    @UseGuards(JwtAuthGuard)
    async publishArtwork(@Request() req, @Param('id') id: string) {
        return this.artworksService.updatePublishStatus(id, req.user.userId, true);
    }

    @Patch(':id/unpublish')
    @UseGuards(JwtAuthGuard)
    async unpublishArtwork(@Request() req, @Param('id') id: string) {
        return this.artworksService.updatePublishStatus(id, req.user.userId, false);
    }

    @Patch(':id/feature')
    @UseGuards(JwtAuthGuard, RolesGuard)
    @Roles(UserRole.ADMIN)
    async featureArtwork(@Param('id') id: string) {
        return this.artworksService.updateFeatureStatus(id, true);
    }

    @Patch(':id/unfeature')
    @UseGuards(JwtAuthGuard, RolesGuard)
    @Roles(UserRole.ADMIN)
    async unfeatureArtwork(@Param('id') id: string) {
        return this.artworksService.updateFeatureStatus(id, false);
    }

    @Post(':id/view')
    @Public()
    async recordView(@Param('id') id: string) {
        await this.artworksService.incrementViewCount(id);
        return { success: true };
    }
} 